/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.tbjd.client.beans;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>A Resource represents a person who could be associated to a {@link org.tbjd.client.beans.Task task}</p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 *
 */
public class Resource {

	private int id;
	private String name;
	private String initials;
	private List<Task> tasks = new ArrayList<Task>();
	private int ratio;

	/**
	 * <p> This method allows to add a new bounded task</p>
	 * @param taskId
	 */
	public void addTask(Task task){
		this.tasks.add(task);
		
		// mirror method
		task.addResourceMirror(this);
	}

	/**
	 * <p>Sets a list of {@link org.tbjd.client.beans.Task tasks}</p>
	 * @param taskId
	 */
	public void addTasks(List<Task> tasks) {
		this.tasks.addAll(tasks);
		
		// mirror method
		for (Task task : tasks) {
			task.addResourceMirror(this);
		}
	}

	/**
	 * <p>Sets a unique {@link org.tbjd.client.beans.Task task}</p>
	 * <p>Avoid creating a list to set only one task id</p>
	 * @param taskId
	 */
	public void setTask(Task task) {
		this.tasks.clear();
		this.tasks.add(task);
		
		// mirror method
		task.addResourceMirror(this);
	}

	/**
	 * Sets a list of {@link org.tbjd.client.beans.Task tasks}
	 * @param tasks
	 */
	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
		
		// mirror method
		for (Task task : tasks) {
			task.addResourceMirror(this);
		}
	}
	
	/**
	 * This method allows to remove a {@link org.tbjd.client.beans.Task task}
	 * @param taskId
	 */
	public void removeTask(Task task){
		if (this.tasks != null){
			this.tasks.remove(task);
		}
		
		// mirror method
		task.removeResourceMirror(this);
	}
	
	/**
	 * This method allows to remove a list of {@link org.tbjd.client.beans.Task tasks}
	 * @param taskId
	 */
	public void removeTasks(List<Task> tasks){
		if (this.tasks != null){
			this.tasks.removeAll(tasks);
		}
		
		// mirror method
		for (Task task : tasks) {
			task.removeResourceMirror(this);
		}
	}

	/**
	 * Called when a Task has added this Resource to its list
	 * @param task
	 */
	public void addTaskMirror(Task task) {
		this.tasks.add(task);
	}

	public void removeTaskMirror(Task task) {
		if (this.tasks != null){
			this.tasks.remove(task);
		}
	}

	/**
	 * Returns the list of related {@link org.tbjd.client.beans.Task tasks}
	 * @return the list of Tasks's ids related to the Resource
	 */
	public List<Task> getTasks() {
		return this.tasks;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getRatio() {
		return ratio;
	}

	public void setRatio(int ratio) {
		this.ratio = ratio;
	}

	public String getInitials() {
		return initials;
	}

	public void setInitials(String initials) {
		this.initials = initials;
	}

	public String toString() {
		super.toString();
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		sb.append(getId());
		sb.append("] ");
		sb.append(getName());
		sb.append(" ");
		sb.append(getInitials());
		sb.append(" ");
		sb.append(getRatio());
		sb.append("% ");

		return sb.toString();
	}

}
